package com.netsdk.web.controller;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @Author aqie
 * @Date 2022/3/26 11:24
 * @desc
 */
@Controller
@RequestMapping("sse")
public class SSEController {
    @GetMapping("index")
    public String index() {
        return "sse/index";
    }

    @ResponseBody
    @RequestMapping(value = "/post", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> post(){
        Flux<String> result = Flux.fromStream(IntStream.range(1,5).mapToObj(i -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
            }
            return "flux data--" + i;
        }));
        return result;
    }
}
